package com.elinkway.infinitemovies.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.elinkway.infinitemovies.adapter.FeedBackFragmentPagerAdapter;
import com.elinkway.infinitemovies.adapter.ReportPhotoAdapter;
import com.elinkway.infinitemovies.bigdata.BigDataConstants;
import com.elinkway.infinitemovies.ui.fragment.CopyrightFragment;
import com.elinkway.infinitemovies.ui.fragment.UserFeedBackFragment;
import com.elinkway.infinitemovies.utils.BitmapUtils;
import com.elinkway.infinitemovies.utils.FileUtils;
import com.elinkway.infinitemovies.utils.LogCat;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.ToastUtil;
import com.elinkway.infinitemovies.utils.UmengEventPoster;
import com.elinkway.infinitemovies.view.PagerSlidingTabStrip;
import com.le123.ysdq.R;

import java.util.ArrayList;

/**
 * Created by zhaopeipei on 2016/4/27.
 */
public class ReportActivity extends BaseActionBarActivity{

    private static final String TAG = "ReportActivity";

    private static final int TYPE_RIGHT = 0;

    private static final int TYPE_NO_PROBLEM_TYPE = 2;

    private static final int TYPE_NO_PROBLEM_DESCRIPTION = 1;

    private static final int TYPE_NO_IMAGE = 3;

    private static final int TYPE_NOTHING_WRITE = 4;

    public final static String NEWIMGURL = "newimgurl";

    private static final int TAKE_PICTURE = 1111;
    private static final int GET_PICTURE = 1112;
    private int mSuccessNumber;

    private GridView mGridItemPhotoView;
    private ReportPhotoAdapter mPhotoAdapter;
    public static Bitmap bitmap;
    private PopupWindow mPop;
    private LinearLayout mLinearPopupWindow;
    private RelativeLayout mSubmit;
    private View mPopView;
    private RelativeLayout mPopParent;
    private Button mBtCamera;
    private Button mBtFromPicture;
    private Button mCancel;
    private View mParentView;
    private TextView mReportTextView;
    private ArrayList<String> mAllPictureUrl = new ArrayList<>(); // 所有获得的图片链接，中间以逗号隔开
    private String mAid;
    private String mSite;
    private Handler mHandler;
    private PagerSlidingTabStrip mTabs;
    private ViewPager mViewPager;
    private FeedBackFragmentPagerAdapter adapter;
    private Fragment mCurrentFragment;
    private int mCurrentProblemType = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setActionBarActivityClassName(TAG);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_addpic_focused);
        mParentView = getLayoutInflater().inflate(R.layout.activity_report, null);
        setContentView(mParentView);
        initActionBar();
        initView();
        initViewPager();
        initData();
    }

    public void initView() {
        mSubmit = (RelativeLayout) findViewById(R.id.rl_submit);
        mGridItemPhotoView = (GridView) findViewById(R.id.gv_report_grid_photo);
        mPopView = getLayoutInflater().inflate(R.layout.item_popupwindows, null);
        mLinearPopupWindow = (LinearLayout)mPopView.findViewById(R.id.ll_popup);
        mPopParent = (RelativeLayout) mPopView.findViewById(R.id.parent);
        mBtCamera = (Button) mPopView.findViewById(R.id.item_popupwindows_camera);
        mBtFromPicture = (Button) mPopView.findViewById(R.id.item_popupwindows_Photo);
        mCancel = (Button) mPopView.findViewById(R.id.item_popupwindows_cancel);
        mReportTextView = (TextView) findViewById(R.id.tv_make_sure_report);
    }

    public void initViewPager() {
        mTabs = (PagerSlidingTabStrip) findViewById(R.id.feedback_tabs);
        mViewPager = (ViewPager) findViewById(R.id.vp_report_pager);
        adapter = new FeedBackFragmentPagerAdapter(
                getSupportFragmentManager(), this);
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setAdapter(adapter);
        mTabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                LogCat.e(TAG, "onPageScrolled " + position);
            }

            @Override
            public void onPageSelected(int position) {
                LogCat.e(TAG, "onPageSelected " + position);
                if (position == 0 && mTabs != null && mTabs.getTabsTextView(position) != null) {
                    mTabs.setAllTabsTextColor(getResources().getColor(R.color.color_666666));
                    mTabs.getTabsTextView(position).setTextColor(getResources().getColor(R.color.color_ff3599f9));

                } else if (position == 1 && mTabs != null && mTabs.getTabsTextView(position) != null) {
                    mTabs.setAllTabsTextColor(getResources().getColor(R.color.color_666666));
                    mTabs.getTabsTextView(position).setTextColor(getResources().getColor(R.color.color_ff3599f9));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                LogCat.e(TAG, "onPageScrollStateChanged " + state);
            }
        });
        mTabs.setViewPager(mViewPager);
        mTabs.setAllTabsTextColor(getResources().getColor(R.color.color_666666));
        if (mTabs.getTabsTextView(0) != null) {
            mTabs.getTabsTextView(0).setTextColor(getResources().getColor(R.color.color_ff3599f9));
        }
    }

    public void initData(){
        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1: // 点击提交后的状态
                        mReportTextView.setTextColor(getResources().getColor(R.color.report_not_sure));
                        ToastUtil.showShortToast(ReportActivity.this, R.string.report_success);
                        mReportTextView.setText(getResources().getString(R.string.make_report));
                        break;
                    case 2:
//                        ToastUtil.showShortToast(ReportActivity.this, R.string.report_failed);
                        break;
                    case 3: // 提交失败的状态
                        ToastUtil.showShortToast(ReportActivity.this, R.string.report_failed);
                        mReportTextView.setTextColor(getResources().getColor(R.color.report_sure));
                        mReportTextView.setText(getResources().getString(R.string.re_submit));
                        break;
                    case 4: // 不能提交的状态
                        mReportTextView.setTextColor(getResources().getColor(R.color.report_not_sure));
                        mReportTextView.setText(getResources().getString(R.string.make_report));
                        break;
                    case 5: // 可以提交的状态
                        mReportTextView.setTextColor(getResources().getColor(R.color.report_sure));
                        mReportTextView.setText(getResources().getString(R.string.make_report));
                        break;
                }
                super.handleMessage(msg);
            }
        };
        mAid = getIntent().getStringExtra("aid");
        mSite = getIntent().getStringExtra("site");
        mTitleTv.setText(getString(R.string.feedback));
        mPop = new PopupWindow(this);
        mPop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPop.setBackgroundDrawable(new BitmapDrawable());
        mPop.setFocusable(true);
        mPop.setOutsideTouchable(true);
        mPop.setContentView(mPopView);
        mPopParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPop.dismiss();
                mLinearPopupWindow.clearAnimation();
            }
        });
        mReportTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mReportTextView.isClickable() && mCurrentProblemType == TYPE_RIGHT) {
                    if (mCurrentFragment instanceof CopyrightFragment) {
                        ((CopyrightFragment) mCurrentFragment).submit();
                    }
                    FileUtils.uploadAppLog();
                    UmengEventPoster.doEventPost(BigDataConstants.ILLEGAL_REPORT_BTN_CLICK);
                    mReportTextView.setTextColor(getResources().getColor(R.color.report_not_sure));
                    mReportTextView.setText(getResources().getString(R.string.submitting));
                    mCurrentProblemType = 4;
                } else if (mCurrentProblemType == TYPE_NO_PROBLEM_TYPE) {
                    ToastUtil.showShortToast(ReportActivity.this, R.string.no_feedback_type);
                } else if (mCurrentProblemType == TYPE_NO_PROBLEM_DESCRIPTION) {
                    ToastUtil.showShortToast(ReportActivity.this, R.string.no_feedback_description);
                } else if (mCurrentProblemType == TYPE_NOTHING_WRITE) {
                    ToastUtil.showShortToast(ReportActivity.this, R.string.nothing);
                } else if (mCurrentProblemType == TYPE_NO_IMAGE) {
                    ToastUtil.showShortToast(ReportActivity.this, R.string.add_image);
                }
            }
        });

    }


    @Override
    public void onResume() {
        super.onResume();
        LogUtils.e(TAG, "View Pager CurrentItem " + mViewPager.getCurrentItem());
        if (mViewPager != null && mViewPager.getCurrentItem() == 1) {
            if (mPhotoAdapter != null) {
                mPhotoAdapter.update();
            }
            if (BitmapUtils.selectBitmap.size() > 0) {
                mReportTextView.setTextColor(getResources().getColor(R.color.report_sure));
            } else {
                mReportTextView.setTextColor(getResources().getColor(R.color.report_not_sure));
            }
        } else if (mViewPager != null && mViewPager.getCurrentItem() == 0) {

        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        LogUtils.e("", "onActivityResult");
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void sendMessage(int what) {
        if (mHandler != null)  {
            mHandler.sendEmptyMessage(what);
        }
    }

    public String getAid() {
        return mAid;
    }

    public String getSite() {
        return mSite;
    }

    public void notification(Fragment fragment, boolean weatherSubmit, int type) {
        LogUtils.e(TAG, "notification is " + type);
        mCurrentFragment = fragment;
        mCurrentProblemType = type;
        if (weatherSubmit) {
            mHandler.sendEmptyMessage(5);
        } else {
            mHandler.sendEmptyMessage(4);
        }

    }

    public void changeSubmitButtonState(boolean isVisible) {
        if (isVisible) {
            mSubmit.setVisibility(View.VISIBLE);
        } else {
            mSubmit.setVisibility(View.GONE);
        }
    }

    @Override
    public void dealWithUmeng() {
    }

    @Override
    public void dealWithVisitUmeng() {

    }
}
